<?php

namespace App\Http\Controllers\Home;

// use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
//引用对应的命名空间
use Captcha;
use Session;

class LoginController extends Controller
{
    public function Fromlogin(){
        //print_r(bcrypt('123456'));die;
        return view('home.login');
    }
    public function login_input(Request $request){
        
        //print_r(Input::all());die;获取所有提交的数据 必须use Tlluminate\Support\facades\Input
        //print_r($request->all());die;
        $captcha = $request->input('captcha');
        // 获取验证码
        $milkcaptcha = Session::get('captcha');
        //$users = DB::table('admins')->get();
       //if(!empty($captcha) && $milkcaptcha == $captcha){
        if(captcha_check($request->input('captcha'))){
            $credentials = $request->only('username', 'password');//bcrypt('123456')加密模式
            if (Auth::guard('user')->attempt($credentials)) {
                $model = Auth::guard('user')->user();
                $model->count = $model->count + 1; 
                $model->save();
                // 身份验证通过...
                $data = ['status'=>1,'msg'=>'登录成功！'];
                return $data;
                //return redirect()->intended('/admin/index');跳转页面
            }else{
                $data = ['status'=>0,'msg'=>'用户名或密码有误！'];
                return $data;
            }
        }else{
            //return back()->with('errors', '验证码有误！');
            $data = ['status'=>0,'msg'=>'验证码有误！'];
            return $data;
        }

    }
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function captcha($tmp)
    {
                //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();
 
        //把内容存入session
        Session::flash('milkcaptcha', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");//清除缓存
        header('Content-Type: image/jpeg');
        $builder->output();
    }
    public function logout(){
        Auth::guard('user')->logout();
        return redirect('/home/login');
    }
}
